/**
 * DEPRECATED
 * 
 * Embeded script file that is embeded from the content script 
 * into the web page to test the global window object for 
 * javascript libraries.
 * 
 * There's one catch in some libraries: 
 * If the developer renamed the scope of the Library (i.e. window.dojo) 
 * to something entirely different, the test fails.
 * 
 * Currently the script tests for the following libraries:
 * 	-jQuery
 * 	-jQuery UI
 * 	-dojo
 * 	-ExJs
 * 	-YUI
 * 	-GWT
 * 	-Prototype
 * 	-Scriptaculous
 * 	-MooTools
 * 	-Modernizr
 * 
 * If one of these is found on the current page, it stores the library and 
 * it's version into an object, that is later stored into the localStorage 
 * of the page. If no library is detected an empty string is stored into the 
 * localStorage.
 * 
 */

;(function(){
	var localStorageKey = "LD",
		oldStorageValue = localStorage[localStorageKey],
		libs = {
			"jQuery" : {
				icon : "jquery",
				test : function(win) {
					var jq = win.jQuery || win.$ || win.$jq || win.$j;
					if(jq && jq.fn && jq.fn.jquery) {
						return { version: jq.fn.jquery };
					} else {
						return false;
					}
				}
			},
			
			"jQuery UI" : {
				icon : "jquery-ui",
				test: function(win) {
					
					var jq = win.jQuery || win.$ || win.$jq || win.$j;
					if(jq && jq.fn && jq.fn.jquery && jq.ui) {

						var plugins = 'accordion,datepicker,dialog,draggable,droppable,progressbar,resizable,selectable,slider,menu,grid,tabs'.split(','), concat = [];
						for (var i=0; i < plugins.length; i++) { if(jq.ui[plugins[i]]) concat.push(plugins[i].substr(0,1).toUpperCase() + plugins[i].substr(1)); };
					
						return { version: jq.ui.version || undefined, details: concat.length ? 'Plugins used: '+concat.join(',') : '' };
					} else {
						return false;
					}
					
				}
			},
			
			"jQuery Mobile" : {
				icon : "jquery-mobile",
				test: function(win) {
					
					var jq = win.jQuery || win.$ || win.$jq || win.$j;
					if(jq && jq.fn && jq.fn.jquery && jq.mobile) {
						return { version: undefined };
					} else {
						return false;
					}
					
				}
			},
			
			"Dojo" : {
				icon : "dojo",
				test : function(win) {
					if(win.dojo || win.djConfig) {
						return win.dojo ? { 
							version: win.dojo.version.toString(), 
							details: 'Details: '+(win.dijit ? 'Uses Dijit' : 'none')
						} : {
							version: undefined
						};
					} else {
						return false;
					}
				}
			},
			
			"Prototype" : {
				icon : "prototype",
				test : function(win) {
					if(win.Prototype && win.Prototype.Version) {
						return { version: win.Prototype.Version };
					} else {
						return false;
					}
				}
			},
			
			"Scriptaculous" : {
				icon : "scriptaculous",
				test : function(win) {
					if(win.Scriptaculous && win.Scriptaculous.Version) {
						return { version: win.Scriptaculous.Version };
					} else {
						return false;
					}
				}
			},
			
			"MooTools" : {
				icon : "mootools",
				test : function(win) {
					if(win.MooTools && win.MooTools.version) {
						return { version: win.MooTools.version };
					} else {
						return false;
					}
				}
			},
			
			"YUI" : {
				icon : "yui",
				test : function(win) {
					if((win.YAHOO && win.YAHOO.VERSION) || (win.YUI && win.YUI.version)) {
						return { version: win.YAHOO.VERSION || win.YUI.version};
					} else {
						return false;
					}
				}
			},
			
			"Sencha" : {
				icon : "sencha",
				test : function(win) {
					if(win.Ext && win.Ext.version) {
						return { version: win.Ext.version };
					} else {
						return false;
					}
				}
			},
			
			"Modernizr" : {
				icon : "modernizr",
				test : function(win) {
					if(win.Modernizr) {
						return { version: win.Modernizr._version };
					}
					return false;
				}
			},

			"GWT" : {
				icon : "gwt",
				test : function(win) {
					return win.__gwt_scriptsLoaded ? {version: undefined } : false;
				}
			},
			
			"Spry JS" : {
				icon : "spry_js",
				test : function(win) {
					if(win.Spry) {
						return { version: undefined };
					} else {
						return false;
					}
				}
			},
			
			"Qooxdoo" : {
				icon : "qooxdoo",
				test : function(win) {
					if(win.qx && win.qx.Bootstrap) {
						return { version: undefined };
					} else {
						return false;
					}
				}
			},
			
			"base2.DOM" : {
				test : function(win) {
					if(win.base2 && win.base2.version) {
						return { version: win.base2.version };
					} else {
						return false;
					}
				}
			},
			
			"Closure" : {
				icon : "closure",
				test : function(win) {
					if(win.goog) {
						return { version: '2.0' };
					}
					return false;
				}
			},
			
			"Raphael" : {
				icon : "raphael",
				test : function(win) {
					if(win.Raphael) {
						return { version: win.Raphael.version };
					}
					return false;
				}
			},
			
			"Processing.js" : {
				icon : "processing",
				test : function(win) {
					if(win.Processing) {
						return { version: win.Processing.version };
					}
					return false;
				}
			},
			
			"Backbone.js" : {
				icon : "backbone",
				test : function(win) {
					if (win.Backbone) {
						return {version: win.Backbone.VERSION};
					}
					return false;
				}
			},
			
			"Underscore.js" : {
				test : function(win) {
					// *should* be safeish for sites that have assigned a generic "_" to something else
					if (win._ && win._.VERSION && typeof win._.tap === 'function') {
						return {version: win._.VERSION};
					}
					return false;
				}
			},
			
			"Sammy.js" : {
				icon : "sammyjs",
				test : function(win) {
					if (win.Sammy && win.Sammy.VERSION) {
						return {version: win.Sammy.VERSION};
					}
					return false;
				}
			},
			
			"Rico" : {
				test : function(win) {
					if (win.Rico && win.Rico.Version) {
						return {version: win.Rico.Version};
					}
					return false;
				}
			},
			
			"MochiKit" : {
				icon : "mochikit",
				test : function(win) {
					if (win.MochiKit) {
						return {version: win.MochiKit.VERSION};	
					}
					return false;
				}
			},
			
			"gRaphael" : {
				icon : "gRaphael",
				test : function(win) {
					if (win.Raphael && win.Raphael.fn.g) {
						return {version: undefined};
					}
					return false;
				}
			},
			
			"Glow" : {
				icon : "glow",
				test : function(win) {
					if (win.gloader) {
						return {version: undefined};
					}
					else if (win.glow) {
						return {version: win.glow.VERSION};
					}
					else if (win.Glow) {
						return {version: win.Glow.version};
					}
					return false;
				}
			},
			
			"Socket.IO" : {
				test : function(win) {
					if (win.io) {
						return {version: win.io.version};
					}
					return false;
				}
			},
			
			"Mustache.js" : {
				icon : "mustache",
				test : function(win) {
					if (win.Mustache) {
						return {version: win.Mustache.version};
					}
					return false;
				}
			},
			
			"Fabric.js" : {
				test : function(win) {
					if (win.fabric) {
						return {version: win.fabric.version};
					}
					return false;
				}
			},
			
			"FuseJS" : {
				icon : "fuseJS",
				test : function(win) {
					if (win.fuse) {
						return {version: win.fuse.version};
					}
					return false;
				}
			},
			
			"Tween.js" : {
				test : function(win) {
					if (win.TWEEN) {
						return {version: undefined};
					}
					return false;
				}
			},
			
			"SproutCore" : {
				icon : "SproutCore",
				test : function(win) {
					if (win.SC) {
						return {version: undefined};
					}
					return false;
				}
			},
			
			"Zepto.js" : {
				icon : "Zeptojs",
				test : function(win) {
					if (win.Zepto && win.Zepto.fn) {
						return {version: undefined};
					}
					return false;
				}
			},
			
			"three.js" : {
				test : function(win) {
					if (win.THREE) {
						return {version: undefined};
					}
					return false;
				}
			},
			
			"OpenAjaxHub" : {
				test : function(win) {
					if(win.OpenAjax) {
						if (win.OpenAjax.hub && win.OpenAjax.hub.implVersion) {
							return { version: win.OpenAjax.hub.implVersion };
						} else {
							return { version: undefined };
						}
					}
					return false;
				}
			},
			
			"Lightstreamer" : {
				icon : "Lightstreamer",
				test : function(win) {
					if(win.Lightstreamer && win.Lightstreamer.version) {
						return { version: win.Lightstreamer.version };
					} else if (win.PushPage && win.NonVisualTable) { //<=4.3
						return { version: undefined };
					}
					return false;
				}
			},
			
			"Ally" : {
				icon : "Ally",
				test : function(win) {
					if (win.Ally && win.Ally.version) {
						return {version: win.Ally.version}
					}
					return false
				}
			}
	},
	libs_passed = {};
	
	/**
	 * 
	 */
	function triggerStorageEvent() {
		var storageEvent = document.createEvent("StorageEvent");
		storageEvent.initStorageEvent("storage", false, false, "LD", oldStorageValue, localStorage[localStorageKey], null, null);
		document.body.dispatchEvent(storageEvent);
	}
	
	/**
	 * 
	 */
	function perform_tests() {
		//ensure that the dom is completely loaded
		if (window.performance.timing.loadEventEnd === 0) {
			window.setTimeout(perform_tests, 50);
			return;
		} else {
			var lib, version, i = 0, stringify = "";
			
			for (lib in libs) {
				if (libs[lib].test && (version = libs[lib].test(window))) {
					libs_passed[lib] = {
							version : version.version
					};
					
					if (libs[lib].icon) {
						libs_passed[lib].icon = libs[lib].icon;
					}
				};
			};
			
			//base2.DOM overwrites the JSON object
			//and the code throws an error
			if (!JSON.stringify) {
				return;
			}
			stringify = JSON.stringify(libs_passed);
			
			//set localStorage
			localStorage.setItem(localStorageKey, stringify === "{}" ? "" : stringify);
			/**
			 * Trigger manually onstorage event as currently it is not 
			 * triggered in Chrome, it is not supported.
			 */
			if (localStorage["onstorage"] === undefined) {
				triggerStorageEvent();
			}
		}
	}
	
	/**
	 * 
	 */
	function init() {
		//start testing
		perform_tests();
	}
	
	init();
})();
